package com.online.questionnaire.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.online.questionnaire.common.ErrorCode;
import com.online.questionnaire.common.PageParam;
import com.online.questionnaire.common.R;
import com.online.questionnaire.common.ResultUtils;
import com.online.questionnaire.exception.BusinessException;
import com.online.questionnaire.exception.ThrowUtils;
import com.online.questionnaire.model.User;
import com.online.questionnaire.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;


/**
 * 通用控制器
 */
@Slf4j
public abstract class BaseController<T> {

    //子类实现
    protected abstract IService<T> getService();
    protected abstract UserService getUserService();

    /**
     * 分页查询
     * @param param
     * @param obj
     * @return
     */
    @GetMapping("/page")
    public R<IPage<T>> page(PageParam param, T obj, HttpServletRequest request) {
        if (param == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        if (param.getPage() == null || param.getPage() <= 0){
            param.setPage(1);
        }
        if (param.getSize() == null || param.getSize() <= 0 || param.getSize() > 100){
            param.setSize(10);
        }

        QueryWrapper<T> queryWrapper = new QueryWrapper<>(obj);
        IPage<T> page = getService().page(new Page(param.getPage(), param.getSize()), queryWrapper);
        return ResultUtils.success(page);
    }


    /**
     * 新增操作
     */
    @PostMapping("/add")
    public R<Boolean> add(@RequestBody T obj,HttpServletRequest request) {
        if (obj == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean save = getService().save(obj);
        if (!save){
            return ResultUtils.error(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(save);
    }

    /**
     * 修改操作
     */
    @PostMapping("/update")
    public R<Boolean> update(@RequestBody T obj,HttpServletRequest request) {
        if (obj == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = getUserService().getLoginUser(request);
        if (loginUser == null){
            ThrowUtils.notLoginError();
        }
        boolean update = getService().updateById(obj);
        if (!update){
            return ResultUtils.error(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(update);
    }

    /**
     * 删除操作 不安全 禁止继承
     */
//    @GetMapping("/delete/{id}")
//    public R<Boolean> delete(@PathVariable Long id, HttpServletRequest request) {
//        if (id == null){
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
//        boolean remove = getService().removeById(id);
//        if (!remove){
//            return ResultUtils.error(ErrorCode.OPERATION_ERROR);
//        }
//        return ResultUtils.success(remove);
//    }

}
